@using System.Globalization
@using BTCPayServer.Abstractions.Contracts
@using BTCPayServer.Client
@using BTCPayServer.TagHelpers
@using BTCPayServer.Views.Apps
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using BTCPayServer.Forms
@using BTCPayServer.Plugins.Crowdfund
@inject FormDataService FormDataService
@inject IFileService FileService
@inject BTCPayServer.Security.ContentSecurityPolicies Csp
@model BTCPayServer.Plugins.Crowdfund.Models.UpdateCrowdfundViewModel
@{
    // layout-menu-item="@nameof(CrowdfundPlugin)-@app.Id"
    ViewData.SetLayoutModel(new LayoutModel($"{nameof(CrowdfundPlugin)}-{Model.AppId}", StringLocalizer["Update Crowdfund"]));
	Csp.UnsafeEval();
    var canUpload = await FileService.IsAvailable();
    var checkoutFormOptions = await FormDataService.GetSelect(Model.StoreId, Model.FormId);
}

@section PageHeadContent {
    <link href="~/vendor/summernote/summernote-bs5.css" rel="stylesheet" asp-append-version="true" />
    <style>.flatpickr-wrapper { flex-grow: 1; }</style>
}

@section PageFootContent {
    <partial name="_ValidationScriptsPartial" />
    <script src="~/vendor/summernote/summernote-bs5.js" asp-append-version="true"></script>
    <script src="~/crowdfund/admin.js" asp-append-version="true"></script>
}

<form method="post" enctype="multipart/form-data" permissioned="@Policies.CanModifyStoreSettings">
    <div class="sticky-header">
        <h2>@ViewData["Title"]</h2>
        <div>
            <button id="page-primary" type="submit" class="btn btn-primary order-sm-1">Save</button>
            <a class="btn btn-secondary" asp-action="ListInvoices" asp-controller="UIInvoice" asp-route-storeId="@Model.StoreId" asp-route-searchterm="@Model.SearchTerm">Invoices</a>
			@if (Model.Archived)
            {
                <button type="submit" class="btn btn-outline-secondary" name="Archived" value="False" text-translate="true">Unarchive</button>
            }
            else if (Model.ModelWithMinimumData)
            {
                <a class="btn btn-secondary" asp-controller="UICrowdfund" asp-action="ViewCrowdfund" asp-route-appId="@Model.AppId" id="ViewApp" target="_blank" text-translate="true">View</a>
            }
        </div>
    </div>

    <partial name="_StatusMessage" />

    <input type="hidden" asp-for="StoreId" />
    <input type="hidden" asp-for="Archived" />

    @if (!ViewContext.ModelState.IsValid)
    {
        <div asp-validation-summary="All" class="@(ViewContext.ModelState.ErrorCount.Equals(1) ? "no-marker" : "")"></div>
    }
    <div class="row" style="max-width:540px;">
        <div class="col-sm-6">
            <div class="form-group">
                <label asp-for="AppName" class="form-label" data-required></label>
                <input asp-for="AppName" class="form-control" required />
                <span asp-validation-for="AppName" class="text-danger"></span>
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <label asp-for="Title" class="form-label" data-required></label>
                <input asp-for="Title" class="form-control" required />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="Tagline" class="form-label"></label>
            <input asp-for="Tagline" class="form-control" />
            <span asp-validation-for="Tagline" class="text-danger"></span>
        </div>
        <div class="form-group">
            <div class="d-flex align-items-center justify-content-between gap-2">
                <label asp-for="MainImageFile" class="form-label"></label>
                @if (!string.IsNullOrEmpty(Model.MainImageUrl))
                {
                    <button type="submit" class="btn btn-link p-0 text-danger" name="RemoveLogoFile" value="true">
                        <vc:icon symbol="cross" /> <span text-translate="true">Remove</span>
                    </button>
                }
            </div>
            @if (canUpload)
            {
                <div class="d-flex align-items-center gap-3">
                    <input asp-for="MainImageFile" type="file" class="form-control flex-grow">
                    @if (!string.IsNullOrEmpty(Model.MainImageUrl))
                    {
                        <img src="@Model.MainImageUrl" alt="Logo" style="height:2.1rem;max-width:10.5rem;" />
                    }
                </div>
                <span asp-validation-for="MainImageFile" class="text-danger"></span>
            }
            else
            {
                <input asp-for="MainImageFile" type="file" class="form-control" disabled>
                <div class="form-text">@ViewLocalizer["In order to upload, a {0} must be configured.", Html.ActionLink(StringLocalizer["file storage"], "Files", "UIServer")]</div>
            }
        </div>
        <div class="form-group">
            <div class="d-flex align-items-center">
                <input asp-for="Enabled" type="checkbox" class="btcpay-toggle me-3"/>
                <div>
                    <label asp-for="Enabled" class="form-check-label"></label>
                    <span asp-validation-for="Enabled" class="text-danger"></span>
                    <div class="text-muted" text-translate="true">The crowdfund will be visible to anyone.</div>
                </div>
            </div>
        </div>
    </div>
    <div class="row mt-3">
        <div class="col-xxl-constrain">
            <div class="form-group">
                <label asp-for="Description" class="form-label" data-required></label>
                <textarea asp-for="Description" rows="20" cols="40" class="form-control richtext"></textarea>
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xl-10 col-xxl-constrain">
            <h3 class="mt-5 mb-4">Goal</h3>
            <div class="d-flex flex-wrap gap-3 mb-3">
                <div class="form-group w-250px mb-0">
                    <label asp-for="TargetAmount" class="form-label"></label>
                    <input inputmode="decimal" asp-for="TargetAmount" class="form-control" />
                    <span asp-validation-for="TargetAmount" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="TargetCurrency" class="form-label"></label>
                    <input asp-for="TargetCurrency" class="form-control w-auto" currency-selection />
                    <div class="form-text">@StringLocalizer["Uses the store's default currency ({0}) if empty.", @Model.StoreDefaultCurrency]</div>
                    <span asp-validation-for="TargetCurrency" class="text-danger"></span>
                </div>
            </div>
            <div class="d-flex flex-wrap gap-3 align-items-center mb-4">
                <div class="form-group mb-0 w-250px">
                    <label asp-for="StartDate" class="form-label"></label>
                    <div class="input-group flex-nowrap">
                        <input type="datetime-local" asp-for="StartDate"
                               value="@(Model.StartDate?.ToString("u", CultureInfo.InvariantCulture))"
                               class="form-control flatdtpicker"
                               placeholder="@StringLocalizer["No start date has been set"]" />
                        <button class="btn btn-secondary input-group-clear px-3" type="button" title="Clear">
                            <vc:icon symbol="close"/>
                        </button>
                    </div>
                </div>
                <div class="form-group mb-0 w-250px">
                    <label asp-for="EndDate" class="form-label"></label>
                    <div class="input-group flex-nowrap">
                        <input type="datetime-local" asp-for="EndDate"
                               value="@(Model.EndDate?.ToString("u", CultureInfo.InvariantCulture))"
                               class="form-control flatdtpicker"
                               placeholder="@StringLocalizer["No end date has been set"]" />
                        <button class="btn btn-secondary input-group-clear px-3" type="button" title="Clear">
                            <vc:icon symbol="close"/>
                        </button>
                    </div>
                </div>
                <span asp-validation-for="StartDate" class="text-danger"></span>
                <span asp-validation-for="EndDate" class="text-danger"></span>
            </div>

            <div class="form-group mt-4" id="ResetRow" hidden="@(Model.StartDate == null)">
                <div class="d-flex align-items-center mb-3">
                    <input asp-for="IsRecurring" type="checkbox" class="btcpay-toggle me-3" data-bs-toggle="collapse" data-bs-target="#ResetEverySettings" aria-expanded="@(Model.IsRecurring)" aria-controls="ResetEverySettings" />
                    <div>
                        <label asp-for="IsRecurring" class="form-check-label" text-translate="true">Recurring Goal</label>
                        <span asp-validation-for="IsRecurring" class="text-danger"></span>
                        <div class="text-muted" text-translate="true">Reset goal after a specific period of time, based on your crowdfund's start date.</div>
                    </div>
                </div>

                <div class="collapse @(Model.IsRecurring ? "show" : "")" id="ResetEverySettings">
                    <div class="form-group mb-0 pt-2 w-250px">
                        <label asp-for="ResetEveryAmount" class="form-label"></label>
                        <div class="d-flex align-items-center">
                            <input type="number" inputmode="numeric" asp-for="ResetEveryAmount" placeholder="@StringLocalizer["Amount"]" class="form-control me-3" min="0">
                            <select class="form-select w-auto" asp-for="ResetEvery">
                                @foreach (var opt in Model.ResetEveryValues)
                                {
                                    <option value="@opt">@opt</option>
                                }
                            </select>
                        </div>
                        <span asp-validation-for="ResetEveryAmount" class="text-danger"></span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="perks" class="row">
        <div class="col-xxl-constrain">
            <partial name="TemplateEditor" model="@(nameof(Model.PerksTemplate), Model.PerksTemplate, "Perks", Model.TargetCurrency ?? Model.StoreDefaultCurrency)" />
        </div>
    </div>
    <div class="row">
        <div class="col-xl-8 col-xxl-constrain">
            <h3 class="mt-5 mb-4" text-translate="true">Contributions</h3>
            <div class="d-flex mb-3">
                <input asp-for="SortPerksByPopularity" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="SortPerksByPopularity" class="form-check-label"></label>
                <span asp-validation-for="SortPerksByPopularity" class="text-danger"></span>
            </div>
            <div class="d-flex mb-3">
                <input asp-for="DisplayPerksRanking" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="DisplayPerksRanking" class="form-check-label"></label>
                <span asp-validation-for="DisplayPerksRanking" class="text-danger"></span>
            </div>
            <div class="d-flex mb-3">
                <input asp-for="DisplayPerksValue" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="DisplayPerksValue" class="form-check-label"></label>
                <span asp-validation-for="DisplayPerksValue" class="text-danger"></span>
            </div>
            <div class="d-flex mb-3">
                <input asp-for="EnforceTargetAmount" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="EnforceTargetAmount" class="form-check-label"></label>
                <span asp-validation-for="EnforceTargetAmount" class="text-danger"></span>
            </div>

            <h3 class="mt-5 mb-4" text-translate="true">Crowdfund Behavior</h3>
            <div class="d-flex">
                <input asp-for="UseAllStoreInvoices" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="UseAllStoreInvoices" class="form-check-label"></label>
                <span asp-validation-for="UseAllStoreInvoices" class="text-danger"></span>
            </div>

            <h3 class="mt-5 mb-4" text-translate="true">Checkout</h3>
            <div class="form-group">
                <label asp-for="FormId" class="form-label"></label>
                <select asp-for="FormId" class="form-select w-auto" asp-items="@checkoutFormOptions"></select>
                <span asp-validation-for="FormId" class="text-danger"></span>
            </div>

            <h3 class="mt-5 mb-2" text-translate="true">Additional Options</h3>
            <div class="form-group">
                <div class="accordion" id="additional">

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="additional-htmlheader-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#additional-htmlheader" aria-expanded="false" aria-controls="additional-htmlheader">
                                <span text-translate="true">HTML Headers</span>
                                <vc:icon symbol="caret-down" />
                            </button>
                        </h2>
                        <div id="additional-htmlheader" class="accordion-collapse collapse" aria-labelledby="additional-htmlheader-header">
                            <div class="accordion-body">
                                <div class="form-group">
                                    <label asp-for="Language" class="form-label"></label>
                                    <input asp-for="Language" class="form-control" maxlength="2" required />
                                    <div class="form-text">Fix the HTML page language</div>
                                    <span asp-validation-for="Language" class="text-danger"></span>
                                </div>
                                <div class="form-group">
                                    <label asp-for="HtmlMetaTags" class="form-label"></label>
                                    <textarea asp-for="HtmlMetaTags" rows="5" cols="40" class="form-control"
                                              placeholder='<meta name="description" content="Your description">
<meta name="keywords" content="keyword1, keyword2, keyword3">
<meta name="author" content="John Doe">
Please insert valid HTML here. Only meta tags accepted.'>
                                    </textarea>
                                    <span asp-validation-for="HtmlMetaTags" class="text-danger"></span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="accordion-item">
                        <h2 class="accordion-header" id="additional-sound-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#additional-sound" aria-expanded="false" aria-controls="additional-sound">
                                <span text-translate="true">Sound</span>
                                <vc:icon symbol="caret-down" />
                            </button>
                        </h2>
                        <div id="additional-sound" class="accordion-collapse collapse" aria-labelledby="additional-sound-header">
                            <div class="accordion-body">
                                <div class="form-group mb-0">
                                    <div class="d-flex align-items-center">
                                        <input asp-for="SoundsEnabled" type="checkbox" class="btcpay-toggle me-3" data-bs-toggle="collapse" data-bs-target="#SoundsEnabledSettings" aria-expanded="@Model.SoundsEnabled" aria-controls="SoundsEnabledSettings"/>
                                        <label asp-for="SoundsEnabled" class="form-check-label"></label>
                                        <span asp-validation-for="SoundsEnabled" class="text-danger"></span>
                                    </div>
                                </div>
                                <div class="collapse @(Model.SoundsEnabled ? "show" : "")" id="SoundsEnabledSettings">
                                    <div class="form-group mb-0 pt-3">
                                        <label asp-for="Sounds" class="form-label"></label>
                                        <textarea asp-for="Sounds" class="form-control" rows="5"></textarea>
                                        <span asp-validation-for="Sounds" class="text-danger"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="additional-animation-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#additional-animation" aria-expanded="false" aria-controls="additional-animation">
                                <span text-translate="true">Animation</span>
                                <vc:icon symbol="caret-down" />
                            </button>
                        </h2>
                        <div id="additional-animation" class="accordion-collapse collapse" aria-labelledby="additional-animation-header">
                            <div class="accordion-body">
                                <div class="form-group mb-0">
                                    <div class="d-flex align-items-center">
                                        <input asp-for="AnimationsEnabled" type="checkbox" class="btcpay-toggle me-3" data-bs-toggle="collapse" data-bs-target="#AnimationsEnabledSettings" aria-expanded="@Model.AnimationsEnabled" aria-controls="AnimationsEnabledSettings"/>
                                        <label asp-for="AnimationsEnabled" class="form-check-label"></label>
                                        <span asp-validation-for="AnimationsEnabled" class="text-danger"></span>
                                    </div>
                                </div>
                                <div class="collapse @(Model.AnimationsEnabled ? "show" : "")" id="AnimationsEnabledSettings">
                                    <div class="form-group mb-0 pt-3">
                                        <label asp-for="AnimationColors" class="form-label"></label>
                                        <textarea asp-for="AnimationColors" class="form-control" rows="5"></textarea>
                                        <span asp-validation-for="AnimationColors" class="text-danger"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="additional-discussion-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#additional-discussion" aria-expanded="false" aria-controls="additional-discussion">
                                <span text-translate="true">Discussion</span>
                                <vc:icon symbol="caret-down" />
                            </button>
                        </h2>
                        <div id="additional-discussion" class="accordion-collapse collapse" aria-labelledby="additional-discussion-header">
                            <div class="accordion-body">
                                <div class="form-group mb-0">
                                    <div class="d-flex align-items-center">
                                        <input asp-for="DisqusEnabled" type="checkbox" class="btcpay-toggle me-3" data-bs-toggle="collapse" data-bs-target="#DisqusEnabledSettings" aria-expanded="@Model.DisqusEnabled" aria-controls="DisqusEnabledSettings"/>
                                        <label asp-for="DisqusEnabled" class="form-check-label"></label>
                                        <span asp-validation-for="DisqusEnabled" class="text-danger"></span>
                                    </div>
                                </div>
                                <div class="collapse @(Model.DisqusEnabled ? "show" : "")" id="DisqusEnabledSettings">
                                    <div class="form-group mb-0 pt-3">
                                        <label asp-for="DisqusShortname" class="form-label"></label>
                                        <input asp-for="DisqusShortname" class="form-control" />
                                        <span asp-validation-for="DisqusShortname" class="text-danger"></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="additional-notification-header">
                            <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#additional-notification" aria-expanded="false" aria-controls="additional-notification">
                                <span text-translate="true">Notification URL Callbacks</span>
                                <vc:icon symbol="caret-down" />
                            </button>
                        </h2>
                        <div id="additional-notification" class="accordion-collapse collapse" aria-labelledby="additional-notification-header">
                            <div class="accordion-body">
                                <div class="form-group">
                                    <label asp-for="NotificationUrl" class="form-label"></label>
                                    <input asp-for="NotificationUrl" class="form-control" />
                                    <span asp-validation-for="NotificationUrl" class="text-danger"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<div class="d-grid d-sm-flex flex-wrap gap-3 mt-3">
    <form method="post" asp-controller="UIApps" asp-action="ToggleArchive" asp-route-appId="@Model.AppId" permission="@Policies.CanModifyStoreSettings">
        <button type="submit" class="w-100 btn btn-outline-secondary" id="btn-archive-toggle">
            @if (Model.Archived)
            {
                <span class="text-nowrap">Unarchive this app</span>
            }
            else
            {
                <span class="text-nowrap" data-bs-toggle="tooltip" title="Archive this app so that it does not appear in the apps list by default">Archive this app</span>
            }
        </button>
    </form>
    <a id="DeleteApp" class="btn btn-outline-danger" asp-controller="UIApps" asp-action="DeleteApp" asp-route-appId="@Model.AppId" data-bs-toggle="modal" data-bs-target="#ConfirmModal" data-description="The app <strong>@Html.Encode(Model.AppName)</strong> and its settings will be permanently deleted." data-confirm-input="@StringLocalizer["Delete"]" permission="@Policies.CanModifyStoreSettings">Delete this app</a>
</div>

<partial name="_Confirm" model="@(new ConfirmModel(StringLocalizer["Delete app"], StringLocalizer["This app will be removed from this store."], StringLocalizer["Delete"]))" permission="@Policies.CanModifyStoreSettings" />

